home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / ed.init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  19.5 KB  |  699 lines

  1. /* $Header: /u/christos/src/tcsh-6.03/RCS/ed.init.c,v 3.33 1992/10/05 02:41:30 christos Exp $ */
  2. /*
  3.  * ed.init.c: Editor initializations
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTS_ION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: ed.init.c,v 3.33 1992/10/05 02:41:30 christos Exp $")
  40.  
  41. #include "ed.h"
  42. #include "ed.term.h"
  43. #include "tc.h"
  44. #include "ed.defns.h"
  45.  
  46. /* ed.init.c -- init routines for the line editor */
  47. /* #define DEBUG_TTY */
  48.  
  49. int     Tty_raw_mode = 0;    /* Last tty change was to raw mode */
  50. int     MacroLvl = -1;        /* pointer to current macro nesting level; */
  51.                 /* (-1 == none) */
  52. static int Tty_quote_mode = 0;    /* Last tty change was to quote mode */
  53. static unsigned char vdisable;    /* The value of _POSIX_VDISABLE from 
  54.                  * pathconf(2) */
  55.  
  56. int     Tty_eight_bit = -1;    /* does the tty handle eight bits */
  57.  
  58. extern bool GotTermCaps;
  59.  
  60. static ttydata_t extty, edtty, tstty;
  61. #define qutty tstty
  62.  
  63. extern int insource;
  64. #define SHTTY (insource ? OLDSTD : SHIN)
  65.  
  66. static unsigned char ttychars[NN_IO][C_NCC] = {
  67.     {
  68.     CINTR,         CQUIT,      CERASE,        CKILL,    
  69.     CEOF,          CEOL,          CEOL2,        CSWTCH, 
  70.     CDSWTCH,     CERASE2,     CSTART,        CSTOP,
  71.     CWERASE,      CSUSP,      CDSUSP,        CREPRINT,
  72.     CDISCARD,      CLNEXT,     CSTATUS,       CPAGE,
  73.     CPGOFF,         CKILL2,      CBRK,            CMIN,
  74.     CTIME
  75.     },
  76.     {
  77.     CINTR,          CQUIT,       CERASE,        CKILL, 
  78.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  79.     _POSIX_VDISABLE, CERASE2,      CSTART,        CSTOP,        
  80.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  81.     CDISCARD,      _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  82.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 1,
  83.     0
  84.     },
  85.     {    
  86.     0,         0,          0,           0,
  87.     0,         0,          0,           0,
  88.     0,         0,          0,           0,
  89.     0,         0,          0,           0,
  90.     0,         0,          0,           0,
  91.     0,         0,          0,           0,
  92.     0
  93.     }
  94. };
  95.  
  96. #ifdef SIG_WINDOW
  97. void
  98. check_window_size(force)
  99.     int     force;
  100. {
  101. #ifdef BSDSIGS
  102.     sigmask_t omask;
  103. #endif /* BSDSIGS */
  104.     int     lins, cols;
  105.  
  106.     /* don't want to confuse things here */
  107. #ifdef BSDSIGS
  108.     omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
  109. #else /* BSDSIGS */
  110.     (void) sighold(SIG_WINDOW);
  111. #endif /* BSDSIGS */
  112.     /*
  113.      * From: bret@shark.agps.lanl.gov (Bret Thaeler) Avoid sunview bug, where a
  114.      * partially hidden window gets a SIG_WINDOW every time the text is
  115.      * scrolled
  116.      */
  117.     if (GetSize(&lins, &cols) || force) {
  118.     if (GettingInput) {
  119.         ClearLines();
  120.         ClearDisp();
  121.         MoveToLine(0);
  122.         MoveToChar(0);
  123.         ChangeSize(lins, cols);
  124.         Refresh();
  125.     }
  126.     else
  127.         ChangeSize(lins, cols);
  128.     }
  129. #ifdef BSDSIGS
  130.     (void) sigsetmask(omask);    /* can change it again */
  131. #else                /* BSDSIGS */
  132.     (void) sigrelse(SIG_WINDOW);
  133. #endif /* BSDSIGS */
  134. }
  135.  
  136. sigret_t
  137. /*ARGSUSED*/
  138. window_change(snum)
  139. int snum;
  140. {
  141. #ifdef UNRELSIGS 
  142.     /* If we were called as a signal handler, restore it. */
  143.     if (snum > 0)
  144.       sigset(snum, window_change);
  145. #endif /* UNRELSIGS */
  146.     check_window_size(0);
  147. #ifndef SIGVOID
  148.     return (snum);
  149. #endif 
  150. }
  151.  
  152. #endif /* SIG_WINDOW */
  153.  
  154. void
  155. ed_set_tty_eight_bit()
  156. {
  157.     if (tty_getty(SHTTY, &extty) == -1) {
  158. #ifdef DEBUG_TTY
  159.     xprintf("ed_set_tty_eight_bit: tty_getty: %s\n", strerror(errno));
  160. #endif /* DEBUG_TTY */
  161.     return;
  162.     }
  163.     Tty_eight_bit = tty_geteightbit(&extty);
  164. }
  165.  
  166.             
  167. int
  168. ed_Setup(rst)
  169.     int rst;
  170. {
  171.     static int havesetup = 0;
  172.     struct varent *imode;
  173.  
  174.     if (havesetup)     /* if we have never been called */
  175.     return(0);
  176.  
  177. #if defined(POSIX) && defined(_PC_VDISABLE) && !defined(BSD4_4)
  178.     { 
  179.     long pcret;
  180.  
  181.     if ((pcret = fpathconf(SHTTY, _PC_VDISABLE)) == -1L)
  182.         vdisable = (unsigned char) _POSIX_VDISABLE;
  183.     else 
  184.         vdisable = (unsigned char) pcret;
  185.     if (vdisable != (unsigned char) _POSIX_VDISABLE && rst != 0)
  186.         for (rst = 0; rst < C_NCC; rst++) {
  187.         if (ttychars[ED_IO][rst] == (unsigned char) _POSIX_VDISABLE)
  188.             ttychars[ED_IO][rst] = vdisable;
  189.         if (ttychars[EX_IO][rst] == (unsigned char) _POSIX_VDISABLE)
  190.             ttychars[EX_IO][rst] = vdisable;
  191.         }
  192.     }
  193. #else /* ! POSIX || !_PC_VDISABLE && !defined(BSD4_4) */
  194.     vdisable = (unsigned char) _POSIX_VDISABLE;
  195. #endif /* POSIX && _PC_VDISABLE */
  196.     
  197.     if ((imode = adrof(STRinputmode)) != NULL) {
  198.     if (!Strcmp(*(imode->vec), STRinsert))
  199.         inputmode = MODE_INSERT;
  200.     else if (!Strcmp(*(imode->vec), STRoverwrite))
  201.         inputmode = MODE_REPLACE;
  202.     }
  203.     else
  204.     inputmode = MODE_INSERT;
  205.     ed_InitMaps();
  206.     Hist_num = 0;
  207.     Expand = 0;
  208.  
  209.     if (tty_getty(SHTTY, &extty) == -1) {
  210. #ifdef DEBUG_TTY
  211.     xprintf("ed_Setup: tty_getty: %s\n", strerror(errno));
  212. #endif /* DEBUG_TTY */
  213.     return(-1);
  214.     }
  215.  
  216.     tstty = edtty = extty;
  217.  
  218.     T_Speed = tty_getspeed(&extty);
  219.     T_Tabs = tty_gettabs(&extty);
  220.     Tty_eight_bit = tty_geteightbit(&extty);
  221.  
  222. #if defined(POSIX) || defined(TERMIO)
  223.     extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  224.     extty.d_t.c_iflag |=  ttylist[EX_IO][M_INPUT].t_setmask;
  225.  
  226.     extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  227.     extty.d_t.c_oflag |=  ttylist[EX_IO][M_OUTPUT].t_setmask;
  228.  
  229.     extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  230.     extty.d_t.c_cflag |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  231.  
  232.     extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  233.     extty.d_t.c_lflag |=  ttylist[EX_IO][M_LINED].t_setmask;
  234.  
  235. # if defined(IRIX3_3) && SYSVREL < 4
  236.     extty.d_t.c_line = NTTYDISC;
  237. # endif /* IRIX3_3 && SYSVREL < 4 */
  238.  
  239. #else    /* GSTTY */        /* V7, Berkeley style tty */
  240.  
  241.     if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  242.     extty.d_t.sg_flags &= ~(ttylist[EX_IO][M_CONTROL].t_clrmask|XTABS);
  243.     extty.d_t.sg_flags |=   ttylist[EX_IO][M_CONTROL].t_setmask;
  244.     }
  245.     else {
  246.     extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  247.     extty.d_t.sg_flags |= (ttylist[EX_IO][M_CONTROL].t_setmask|XTABS);
  248.     }
  249.  
  250.     extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  251.     extty.d_lb |=  ttylist[EX_IO][M_LOCAL].t_setmask;
  252.  
  253. #endif /* GSTTY */
  254.     /*
  255.      * Reset the tty chars to reasonable defaults
  256.      * If they are disabled, then enable them.
  257.      */
  258.     if (rst) {
  259.     if (tty_cooked_mode(&tstty)) {
  260.         tty_getchar(&tstty, ttychars[TS_IO]);
  261.         /*
  262.          * Don't affect CMIN and CTIME for the editor mode
  263.          */
  264.         for (rst = 0; rst < C_NCC - 2; rst++) 
  265.         if (ttychars[TS_IO][rst] != vdisable &&
  266.             ttychars[ED_IO][rst] != vdisable)
  267.             ttychars[ED_IO][rst] = ttychars[TS_IO][rst];
  268.         for (rst = 0; rst < C_NCC; rst++) 
  269.         if (ttychars[TS_IO][rst] != vdisable &&
  270.             ttychars[EX_IO][rst] != vdisable)
  271.             ttychars[EX_IO][rst] = ttychars[TS_IO][rst];
  272.     }
  273.     tty_setchar(&extty, ttychars[EX_IO]);
  274.     if (tty_setty(SHTTY, &extty) == -1) {
  275. #ifdef DEBUG_TTY
  276.         xprintf("ed_Setup: tty_setty: %s\n", strerror(errno));
  277. #endif /* DEBUG_TTY */
  278.         return(-1);
  279.     }
  280.     }
  281.     else
  282.     tty_setchar(&extty, ttychars[EX_IO]);
  283.  
  284. # ifdef SIG_WINDOW
  285.     (void) sigset(SIG_WINDOW, window_change);    /* for window systems */
  286. # endif 
  287.     havesetup = 1;
  288.     return(0);
  289. }
  290.  
  291. void
  292. ed_Init()
  293. {
  294.     ResetInLine(1);        /* reset the input pointers */
  295.     GettingInput = 0;        /* just in case */
  296.     LastKill = KillBuf;        /* no kill buffer */
  297.  
  298. #ifdef DEBUG_EDIT
  299.     CheckMaps();        /* do a little error checking on key maps */
  300. #endif 
  301.  
  302.     if (ed_Setup(0) == -1)
  303.     return;
  304.  
  305.     /*
  306.      * if we have been called before but GotTermCaps isn't set, our TERM has
  307.      * changed, so get new termcaps and try again
  308.      */
  309.  
  310.     if (!GotTermCaps)
  311.     GetTermCaps();        /* does the obvious, but gets term type each
  312.                  * time */
  313.  
  314. #if defined(TERMIO) || defined(POSIX)
  315.     edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  316.     edtty.d_t.c_iflag |=  ttylist[ED_IO][M_INPUT].t_setmask;
  317.  
  318.     edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  319.     edtty.d_t.c_oflag |=  ttylist[ED_IO][M_OUTPUT].t_setmask;
  320.  
  321.     edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  322.     edtty.d_t.c_cflag |=  ttylist[ED_IO][M_CONTROL].t_setmask;
  323.  
  324.     edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  325.     edtty.d_t.c_lflag |=  ttylist[ED_IO][M_LINED].t_setmask;
  326.  
  327.  
  328. # if defined(IRIX3_3) && SYSVREL < 4
  329.     edtty.d_t.c_line = NTTYDISC;
  330. # endif /* IRIX3_3 && SYSVREL < 4 */
  331.  
  332. #else /* GSTTY */
  333.  
  334.     if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  335.     edtty.d_t.sg_flags &= ~(ttylist[ED_IO][M_CONTROL].t_clrmask | XTABS);
  336.     edtty.d_t.sg_flags |=   ttylist[ED_IO][M_CONTROL].t_setmask;
  337.     }
  338.     else {
  339.     edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  340.     edtty.d_t.sg_flags |= (ttylist[ED_IO][M_CONTROL].t_setmask | XTABS);
  341.     }
  342.  
  343.     edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  344.     edtty.d_lb |=  ttylist[ED_IO][M_LOCAL].t_setmask;
  345. #endif /* POSIX || TERMIO */
  346.  
  347.     tty_setchar(&edtty, ttychars[ED_IO]);
  348. }
  349.  
  350. /* 
  351.  * Check and re-init the line. set the terminal into 1 char at a time mode.
  352.  */
  353. int
  354. Rawmode()
  355. {
  356.     if (Tty_raw_mode)
  357.     return (0);
  358.  
  359. #ifdef _IBMR2
  360.     tty_setdisc(SHTTY, ED_IO);
  361. #endif /* _IBMR2 */
  362.  
  363.     if (tty_getty(SHTTY, &tstty) == -1) {
  364. #ifdef DEBUG_TTY
  365.     xprintf("Rawmode: tty_getty: %s\n", strerror(errno));
  366. #endif /* DEBUG_TTY */
  367.     return(-1);
  368.     }
  369.  
  370.     /*
  371.      * We always keep up with the eight bit setting and the speed of the
  372.      * tty. But only we only believe changes that are made to cooked mode!
  373.      */
  374. #if defined(POSIX) || defined(TERMIO)
  375.     Tty_eight_bit = tty_geteightbit(&tstty);
  376.     T_Speed = tty_getspeed(&tstty);
  377.  
  378. # ifdef POSIX
  379.     /*
  380.      * Fix from: Steven (Steve) B. Green <xrsbg@charney.gsfc.nasa.gov>
  381.      * Speed was not being set up correctly under POSIX.
  382.      */
  383.     if (tty_getspeed(&extty) != T_Speed || tty_getspeed(&edtty) != T_Speed) {
  384.     (void) cfsetispeed(&extty.d_t, T_Speed);
  385.     (void) cfsetospeed(&extty.d_t, T_Speed);
  386.     (void) cfsetispeed(&edtty.d_t, T_Speed);
  387.     (void) cfsetospeed(&edtty.d_t, T_Speed);
  388.     }
  389. # endif /* POSIX */
  390. #else /* GSTTY */
  391.  
  392.     T_Speed = tty_getspeed(&tstty);
  393.     Tty_eight_bit = tty_geteightbit(&tstty);
  394.  
  395.     if (extty.d_t.sg_ispeed != tstty.d_t.sg_ispeed) {
  396.     extty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  397.     edtty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  398.     }
  399.  
  400.     if (extty.d_t.sg_ospeed != tstty.d_t.sg_ospeed) {
  401.     extty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  402.     edtty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  403.     }
  404. #endif /* POSIX || TERMIO */
  405.  
  406.     if (tty_cooked_mode(&tstty)) {
  407. #if defined(POSIX) || defined(TERMIO)
  408.     if (tstty.d_t.c_cflag != extty.d_t.c_cflag) { 
  409.         extty.d_t.c_cflag  = tstty.d_t.c_cflag;
  410.         extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  411.         extty.d_t.c_cflag |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  412.  
  413.         edtty.d_t.c_cflag  = tstty.d_t.c_cflag;
  414.         edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  415.         edtty.d_t.c_cflag |=  ttylist[ED_IO][M_CONTROL].t_setmask;
  416.     }
  417.  
  418.     if ((tstty.d_t.c_lflag != extty.d_t.c_lflag) &&
  419.         (tstty.d_t.c_lflag != edtty.d_t.c_lflag)) {
  420.         extty.d_t.c_lflag = tstty.d_t.c_lflag;
  421.         extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  422.         extty.d_t.c_lflag |=  ttylist[EX_IO][M_LINED].t_setmask;
  423.  
  424.         edtty.d_t.c_lflag = tstty.d_t.c_lflag;
  425.         edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  426.         edtty.d_t.c_lflag |=  ttylist[ED_IO][M_LINED].t_setmask;
  427.     }
  428.  
  429.     if ((tstty.d_t.c_iflag != extty.d_t.c_iflag) &&
  430.         (tstty.d_t.c_iflag != edtty.d_t.c_iflag)) {
  431.         extty.d_t.c_iflag = tstty.d_t.c_iflag;
  432.         extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  433.         extty.d_t.c_iflag |=  ttylist[EX_IO][M_INPUT].t_setmask;
  434.  
  435.         edtty.d_t.c_iflag = tstty.d_t.c_iflag;
  436.         edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  437.         edtty.d_t.c_iflag |=  ttylist[ED_IO][M_INPUT].t_setmask;
  438.     }
  439.  
  440.     if ((tstty.d_t.c_oflag != extty.d_t.c_oflag) &&
  441.         (tstty.d_t.c_oflag != edtty.d_t.c_oflag)) {
  442.         extty.d_t.c_oflag = tstty.d_t.c_oflag;
  443.         extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  444.         extty.d_t.c_oflag |=  ttylist[EX_IO][M_OUTPUT].t_setmask;
  445.  
  446.         edtty.d_t.c_oflag = tstty.d_t.c_oflag;
  447.         edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  448.         edtty.d_t.c_oflag |=  ttylist[ED_IO][M_OUTPUT].t_setmask;
  449.     }
  450.  
  451.     if (tty_gettabs(&extty) == 0) 
  452.         T_Tabs = 0;
  453.     else 
  454.         T_Tabs = CanWeTab();
  455.  
  456. #else /* GSTTY */
  457.  
  458.     if (((tstty.d_t.sg_flags != extty.d_t.sg_flags) || 
  459.          (tstty.d_lb != extty.d_lb)) &&
  460.         ((tstty.d_t.sg_flags != edtty.d_t.sg_flags) || 
  461.          (tstty.d_lb != edtty.d_lb))) {
  462.  
  463.         extty.d_t.sg_flags = tstty.d_t.sg_flags;
  464.  
  465.         /*
  466.          * re-test for some things here (like maybe the user typed 
  467.          * "stty -tabs"
  468.          */
  469.         if (tty_gettabs(&extty) == 0)
  470.         T_Tabs = 0;
  471.         else 
  472.         T_Tabs = CanWeTab();
  473.  
  474.         extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  475.         extty.d_t.sg_flags |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  476.  
  477.         if (T_Tabs)        /* order of &= and |= is important to XTABS */
  478.         extty.d_t.sg_flags &= ~XTABS;
  479.         else 
  480.         extty.d_t.sg_flags |= XTABS;
  481.  
  482.         extty.d_lb = tstty.d_lb;
  483.         extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  484.         extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
  485.  
  486.         edtty.d_t.sg_flags = extty.d_t.sg_flags;
  487.         if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  488.         edtty.d_t.sg_flags &= 
  489.             ~(ttylist[ED_IO][M_CONTROL].t_clrmask|XTABS);
  490.         edtty.d_t.sg_flags |=   ttylist[ED_IO][M_CONTROL].t_setmask;
  491.         }
  492.         else {
  493.         edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  494.         edtty.d_t.sg_flags |= 
  495.             (ttylist[ED_IO][M_CONTROL].t_setmask|XTABS);
  496.         }
  497.  
  498.         edtty.d_lb = tstty.d_lb;
  499.         edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  500.         edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
  501.     }
  502. # endif /* TERMIO || POSIX */
  503.     {
  504.         extern int didsetty;
  505.         int i;
  506.  
  507.         tty_getchar(&tstty, ttychars[TS_IO]);
  508.         /*
  509.          * Check if the user made any changes.
  510.          * If he did, then propagate the changes to the
  511.          * edit and execute data structures.
  512.          */
  513.         for (i = 0; i < C_NCC; i++)
  514.         if (ttychars[TS_IO][i] != ttychars[EX_IO][i])
  515.             break;
  516.         
  517.         if (i != C_NCC || didsetty) {
  518.         didsetty = 0;
  519.         /*
  520.          * Propagate changes only to the unprotected chars
  521.          * that have been modified just now.
  522.          */
  523.         for (i = 0; i < C_NCC; i++) {
  524.             if (!((ttylist[ED_IO][M_CHAR].t_setmask & C_SH(i))) &&
  525.             (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  526.             ttychars[ED_IO][i] = ttychars[TS_IO][i];
  527.             if (ttylist[ED_IO][M_CHAR].t_clrmask & C_SH(i))
  528.             ttychars[ED_IO][i] = vdisable;
  529.         }
  530.         tty_setchar(&edtty, ttychars[ED_IO]);
  531.  
  532.         for (i = 0; i < C_NCC; i++) {
  533.             if (!((ttylist[EX_IO][M_CHAR].t_setmask & C_SH(i))) &&
  534.             (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  535.             ttychars[EX_IO][i] = ttychars[TS_IO][i];
  536.             if (ttylist[EX_IO][M_CHAR].t_clrmask & C_SH(i))
  537.             ttychars[EX_IO][i] = vdisable;
  538.         }
  539.         tty_setchar(&extty, ttychars[EX_IO]);
  540.         }
  541.  
  542.     }
  543.     }
  544.     if (tty_setty(SHTTY, &edtty) == -1) {
  545. #ifdef DEBUG_TTY
  546.     xprintf("Rawmode: tty_setty: %s\n", strerror(errno));
  547. #endif /* DEBUG_TTY */
  548.     return(-1);
  549.     }
  550.     Tty_raw_mode = 1;
  551.     flush();            /* flush any buffered output */
  552.     return (0);
  553. }
  554.  
  555. int
  556. Cookedmode()
  557. {                /* set tty in normal setup */
  558.     sigret_t(*orig_intr) ();
  559.  
  560. #ifdef _IBMR2
  561.     tty_setdisc(SHTTY, EX_IO);
  562. #endif /* _IBMR2 */
  563.  
  564.     if (!Tty_raw_mode)
  565.     return (0);
  566.  
  567.     /* hold this for reseting tty */
  568. #ifdef BSDSIGS
  569.     orig_intr = (sigret_t (*)()) signal(SIGINT, SIG_IGN);
  570. #else
  571.     orig_intr = (sigret_t (*)()) sigset(SIGINT, SIG_IGN);
  572. #endif /* BSDSIGS */
  573.     if (tty_setty(SHTTY, &extty) == -1) {
  574. #ifdef DEBUG_TTY
  575.     xprintf("Cookedmode: tty_setty: %s\n", strerror(errno));
  576. #endif /* DEBUG_TTY */
  577.     return -1;
  578.     }
  579.     Tty_raw_mode = 0;
  580. #ifdef BSDSIGS
  581.     (void) signal(SIGINT, orig_intr);    /* take these again */
  582. #else
  583.     (void) sigset(SIGINT, orig_intr);    /* take these again */
  584. #endif /* BSDSIGS */
  585.     return (0);
  586. }
  587.  
  588. void
  589. ResetInLine(macro)
  590.     int macro;
  591. {
  592.     Cursor = InputBuf;        /* reset cursor */
  593.     LastChar = InputBuf;
  594.     InputLim = &InputBuf[INBUFSIZE - 2];
  595.     Mark = InputBuf;
  596.     MetaNext = 0;
  597.     CurrentKeyMap = CcKeyMap;
  598.     AltKeyMap = 0;
  599.     Hist_num = 0;
  600.     DoingArg = 0;
  601.     Argument = 1;
  602. #ifdef notdef
  603.     LastKill = KillBuf;        /* no kill buffer */
  604. #endif 
  605.     LastCmd = F_UNASSIGNED;    /* previous command executed */
  606.     if (macro)
  607.     MacroLvl = -1;        /* no currently active macros */
  608. }
  609.  
  610. static Char *Input_Line = NULL;
  611. int
  612. Load_input_line()
  613. {
  614.     long    chrs = 0;
  615.  
  616.     if (Input_Line)
  617.     xfree((ptr_t) Input_Line);
  618.     Input_Line = NULL;
  619.  
  620.     if (Tty_raw_mode)
  621.     return 0;
  622.  
  623. #if defined(FIONREAD) && !defined(OREO)
  624.     (void) ioctl(SHIN, FIONREAD, &chrs);
  625.     if (chrs > 0) {
  626.     char    buf[BUFSIZE];
  627.  
  628.     chrs = read(SHIN, buf, (size_t) min(chrs, BUFSIZE - 1));
  629.     if (chrs > 0) {
  630.         buf[chrs] = '\0';
  631.         Input_Line = Strsave(str2short(buf));
  632.         PushMacro(Input_Line);
  633.     }
  634.     }
  635. #endif  /* FIONREAD && !OREO */
  636.     return chrs > 0;
  637. }
  638.  
  639. /*
  640.  * Bugfix (in Swedish) by:
  641.  * Johan Widen
  642.  * SICS, PO Box 1263, S-163 13 SPANGA, SWEDEN
  643.  * {mcvax,munnari,cernvax,diku,inria,prlb2,penet,ukc,unido}!enea!sics.se!jw
  644.  * Internet: jw@sics.se
  645.  *
  646.  * (via Hans J Albertsson (thanks))
  647.  */
  648. void
  649. QuoteModeOn()
  650. {
  651.     if (MacroLvl >= 0)
  652.     return;
  653.  
  654.     qutty = edtty;
  655.  
  656. #if defined(TERMIO) || defined(POSIX)
  657.     qutty.d_t.c_iflag &= ~ttylist[QU_IO][M_INPUT].t_clrmask;
  658.     qutty.d_t.c_iflag |=  ttylist[QU_IO][M_INPUT].t_setmask;
  659.  
  660.     qutty.d_t.c_oflag &= ~ttylist[QU_IO][M_OUTPUT].t_clrmask;
  661.     qutty.d_t.c_oflag |=  ttylist[QU_IO][M_OUTPUT].t_setmask;
  662.  
  663.     qutty.d_t.c_cflag &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  664.     qutty.d_t.c_cflag |=  ttylist[QU_IO][M_CONTROL].t_setmask;
  665.  
  666.     qutty.d_t.c_lflag &= ~ttylist[QU_IO][M_LINED].t_clrmask;
  667.     qutty.d_t.c_lflag |=  ttylist[QU_IO][M_LINED].t_setmask;
  668. #else /* GSTTY */
  669.     qutty.d_t.sg_flags &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  670.     qutty.d_t.sg_flags |= ttylist[QU_IO][M_CONTROL].t_setmask;
  671.     qutty.d_lb &= ~ttylist[QU_IO][M_LOCAL].t_clrmask;
  672.     qutty.d_lb |= ttylist[QU_IO][M_LOCAL].t_setmask;
  673.  
  674. #endif /* TERMIO || POSIX */
  675.     if (tty_setty(SHTTY, &qutty) == -1) {
  676. #ifdef DEBUG_TTY
  677.     xprintf("QuoteModeOn: tty_setty: %s\n", strerror(errno));
  678. #endif /* DEBUG_TTY */
  679.     return;
  680.     }
  681.     Tty_quote_mode = 1;
  682.     return;
  683. }
  684.  
  685. void
  686. QuoteModeOff()
  687. {
  688.     if (!Tty_quote_mode)
  689.     return;
  690.     Tty_quote_mode = 0;
  691.     if (tty_setty(SHTTY, &edtty) == -1) {
  692. #ifdef DEBUG_TTY
  693.     xprintf("QuoteModeOff: tty_setty: %s\n", strerror(errno));
  694. #endif /* DEBUG_TTY */
  695.     return;
  696.     }
  697.     return;
  698. }
  699.